﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using TowerDefender.Graphics;
using TowerDefender.Data;

namespace TowerDefender.Model
{

    public class Engine
    {
        private Window _window;
        private DispatcherTimer dispatcher;
        private long _elapsedTime = 0;


        public Engine(Window window)
        {
            _window = window;
             dispatcher = new DispatcherTimer();
            this.Configure();
        }

        public long ElapsedTime { get { return _elapsedTime; } }

        public event EventHandler Tick;

        private void Configure()
        {
            dispatcher.Interval = TimeSpan.FromTicks(1);
            dispatcher.Tick += new EventHandler(dispatcher_Tick);
            this.Tick +=new EventHandler(Engine_Tick);
        }

        void dispatcher_Tick(object sender, EventArgs e)
        {
            if (Tick != null)
            {
                Tick(this, e);
            }
        }

        void Engine_Tick(object sender, EventArgs e)
        {
            if (_elapsedTime > long.MaxValue)
            {
                _elapsedTime = 0;
            }

            _elapsedTime++;
        }

        public void Start()
        {
      //      dispatcher.Start();

            string path = System.IO.Directory.GetCurrentDirectory() + "/Resources/Maps/Map1.xml";
            Map map = FileManager.LoadMap(path);

            Canvas canvas = MapManager.CreateMap(_window, map);

            //for (int i = 0; i < 7; i++)
            //{
            //    Peon p = new Peon(map, this, 5 * (8 - i), 300 * i);
            //    PeonManager.CreatePeonGraphics(p, map, canvas);
            //}
            
         //   Tick(this, new EventArgs());
        }

        public void Stop()
        {
            dispatcher.Stop();
        }
    }
}